package com.ice_col.guesssongname.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.ice_col.guesssongname.data.Const;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Random;

/**
 * Created by Administrator on 2015/10/13 0013.
 */
public class Util {

    public static View getView(Context context,int layoutId){
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout=inflater.inflate(layoutId,null);
        return layout;
    }

    public static void saveData(Context context,int level,int coins){

        FileOutputStream fos=null;
        try {
            fos = context.openFileOutput(Const.RECORD_FILE, Context.MODE_PRIVATE);
            DataOutputStream dos=new DataOutputStream(fos);
            dos.writeInt(level);
            dos.writeInt(coins);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }
    }

    public static int[] readData(Context context){
        int record[]={0,Const.TOTAL_COINS};
        FileInputStream fis=null;
        try {
            fis = context.openFileInput(Const.RECORD_FILE);
            DataInputStream dis=new DataInputStream(fis);
            record[0]=dis.readInt();//read game level
            record[1]=dis.readInt();//read coins
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try{
                    fis.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return record;
    }

    public static String generateChineseCharacter(){
        String str="";
        int highPos,lowPos;
        Random random=new Random();
        highPos=176+Math.abs(random.nextInt(39));
        lowPos=161+Math.abs(random.nextInt(93));
        byte[] b=new byte[2];
        b[0]=(Integer.valueOf(highPos)).byteValue();
        b[1]=(Integer.valueOf(lowPos)).byteValue();
        try{
            str=new String(b,"GB2312");
        }catch (Exception e){
            e.printStackTrace();
        }
        return str;
    }

    public static void printCharacter() {
        int highPos, lowPos;
        int highCount = 39;
        int lowCount = 93;
        StringBuffer stringBuffer=new StringBuffer();
        for (int i = 0; i < highCount; i++) {
            for (int j = 0; j < lowCount; j++) {
                highPos = 176 + i;
                lowPos = 161 + j;
                byte[] b = new byte[2];
                b[0] = (Integer.valueOf(highPos)).byteValue();
                b[1] = (Integer.valueOf(lowPos)).byteValue();
                try {
                    stringBuffer.append(new String(b, "GBK"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(stringBuffer.toString());
    }

}
